package com.ss.ugc.android.alphavideoplayer

import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.ss.ugc.android.alpha_player.IMonitor
import com.ss.ugc.android.alpha_player.IPlayerAction
import com.ss.ugc.android.alpha_player.model.ScaleType
import com.ss.ugc.android.alphavideoplayer.databinding.ActivityMainBinding
import com.ss.ugc.android.alphavideoplayer.utils.AssetUtils
import com.ss.ugc.android.alphavideoplayer.utils.PermissionUtils
import java.io.File


/**
 * created by dengzhuoyao on 2020/07/08
 */
class MainActivity : AppCompatActivity() {

    private val TAG = "MainActivity"
    private lateinit var activityMainBinding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        activityMainBinding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(activityMainBinding.root)

        PermissionUtils.verifyStoragePermissions(this)
        AssetUtils.copyFileFromAssetsToInternalStorage(this)
        initVideoGiftView()
    }

    private fun initVideoGiftView() {
        activityMainBinding.videoGiftView.initPlayerController(this, this, playerAction, monitor)
    }

    private val playerAction = object : IPlayerAction {
        override fun onVideoSizeChanged(videoWidth: Int, videoHeight: Int, scaleType: ScaleType) {
            Log.i(TAG,
                "call onVideoSizeChanged(), videoWidth = $videoWidth, videoHeight = $videoHeight, scaleType = $scaleType"
            )
        }

        override fun startAction() {
            Log.i(TAG, "call startAction()")
        }

        override fun endAction() {
            Log.i(TAG, "call endAction")
        }
    }

    private val monitor = object : IMonitor {
        override fun monitor(state: Boolean, playType: String, what: Int, extra: Int, errorInfo: String) {
            Log.i(TAG,
                "call monitor(), state: $state, playType = $playType, what = $what, extra = $extra, errorInfo = $errorInfo"
            )
        }
    }

    fun attachView(v: View) {
        activityMainBinding.videoGiftView.attachView()
    }

    fun detachView(v: View) {
        activityMainBinding.videoGiftView.detachView()
    }

    fun playGift(v: View) {
        val testPath = getResourcePath()
        Log.i("dzy", "play gift file path : $testPath")
        if ("" == testPath) {
            Toast.makeText(this, "please run 'gift_install.sh gift/demoRes' for load alphaVideo resource.", Toast.LENGTH_SHORT).show()
        }
        activityMainBinding.videoGiftView.startVideoGift(testPath)
    }

    private fun getResourcePath(): String {
        val basePath = this.getExternalFilesDir(AssetUtils.ASSET_DIR_NAME)?.absolutePath
        val dirPath = basePath + File.separator
        val dirFile = File(dirPath)
        if (dirFile.exists()) {
            val list = dirFile.listFiles()
            if (!list.isNullOrEmpty()) {
                return list[0].absolutePath
            }
        }
        return ""
    }

    override fun onDestroy() {
        super.onDestroy()
        activityMainBinding.videoGiftView.releasePlayerController()
    }
}
